package com.bestcem.xm.award.service.dto.draw;

import com.bestcem.xm.award.constant.Constants;
import com.bestcem.xm.award.entity.pojo.AwardRecordDO;
import com.bestcem.xm.award.enums.AwardIssueStatusEnum;
import com.bestcem.xm.award.enums.DeliverSourceEnum;
import com.bestcem.xm.award.enums.RecordStatusEnum;
import com.bestcem.xm.award.enums.WinningStatusEnum;
import lombok.Data;
import lombok.experimental.Accessors;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * 抽奖记录
 *
 * @author hang.li <hang.li@idiaoyan.com>
 * @version v1.0
 * @date 2021/7/12 14:47
 */
@Data
@Accessors(chain = true)
public class RecordDTO {

    /**
     * 抽奖记录id
     */
    private String recordId;

    /**
     * 答卷编号
     */
    private Long surveySeq;

    /**
     * 投放来源 {@link DeliverSourceEnum}
     */
    private Integer deliverSource;

    /**
     * 投放名称
     */
    private String deliverName;

    /**
     * 抽奖手机号
     */
    private String phone;

    /**
     * 中奖人姓名
     */
    private String name;

    /**
     * 奖项名称
     */
    private String winName;

    /**
     * 奖项等级
     */
    private Integer winLevel;

    /**
     * 奖项等级描述
     */
    private String levelStr;

    /**
     * 领取时间
     */
    private Date receiveTime;

    /**
     * 领取时间
     */
    private String receiveTimeStr;

    /**
     * 中奖状态
     */
    private Integer status;

    /**
     * 兑奖码
     */
    private String code;

    /**
     * 地址信息
     */
    private AwardRecordDO.Address address;

    /**
     * 数据转换
     *
     * @param record 抽奖记录数据
     * @return RecordDTO
     */
    public static RecordDTO awardRecordToRecordDTO(AwardRecordDO record) {
        RecordDTO recordDTO = new RecordDTO();
        recordDTO.setRecordId(record.getId())
                .setPhone(record.getPhone())
                .setName(record.getName());
        if (Objects.nonNull(record.getAward())) {
            recordDTO.setSurveySeq(record.getAward().getSurveySeq());
            recordDTO.setDeliverName(record.getAward().getDeliverName());
            recordDTO.setDeliverSource(record.getAward().getDeliverSource());
        }
        if (Objects.nonNull(record.getWin())) {
            recordDTO.setWinName(record.getWin().getName())
                    .setCode(record.getWin().getCode())
                    .setWinLevel(record.getWin().getLevel())
                    .setLevelStr(record.getWin().getLevelStr());
            SimpleDateFormat format = new SimpleDateFormat(Constants.DATE_PATTERN);
            recordDTO.setReceiveTime(record.getUpdateTime()).setReceiveTimeStr(format.format(record.getUpdateTime()));
        }
        if (Objects.nonNull(record.getAddress())) {
            recordDTO.setAddress(record.getAddress());
        }
        if (record.getStatus() == RecordStatusEnum.YES.getIndex()) {
            if (record.getWin().getStatus() == AwardIssueStatusEnum.YES.getIndex()
                    || record.getWin().getStatus() == AwardIssueStatusEnum.NO.getIndex()) {
                recordDTO.setStatus(WinningStatusEnum.SENT.getIndex());
            } else {
                recordDTO.setStatus(WinningStatusEnum.WIN.getIndex());
            }
        } else {
            recordDTO.setStatus(WinningStatusEnum.NO_WIN.getIndex());
        }
        return recordDTO;
    }
}